import wx, wx.adv
from wx.lib.wordwrap import wordwrap
import message_log
import regist
import pandas as pd
import home


class LoginFrame(wx.Frame):
    def __init__(self, log):
        wx.Frame.__init__(self, None, -1, u'登录', size=(500, 400),
                          style=wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
        self.log = log
        self.count = 3  # 密码尝试次数
        icon = wx.Icon(name='苦短.jpg', type=wx.BITMAP_TYPE_JPEG)     # 设置图标
        self.SetIcon(icon)
        # image =admin wx.Image('苦短.jpg', wx.BITMAP_TYPE_JPEG).ConvertToBitmap()   # 背景图片
        # wx.StaticBitmap(self, -1, bitmap=image, pos=(0, 0), size=(700, 200))
        self.Center()   # 生成界面在屏幕居中
        self.SetBackgroundColour('white')   # 设置背景色
        font = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False)     # 字体规格
        self.SetFont(font)  # 应用
        self.mb = self.make_menu_bar()  # 创建菜单栏
        wx.StaticText(self, -1, "账号:", pos=(80, 100))     # 静态标签
        wx.StaticText(self, -1, "密码:", pos=(80, 140))
        self.entry_user = wx.TextCtrl(self, -1, size=(200, 30), pos=(130, 100))
        self.entry_pass = wx.TextCtrl(self, -1, size=(200, 30), pos=(130, 140), style=wx.TE_PASSWORD)  # 设置style
        self.btn_login = wx.Button(self, -1, '登录', pos=(130, 240), size=(70, 50))  # 设置按钮
        self.btn_login.SetBackgroundColour('#0a74f7')
        # pic = wx.Image('but_log.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()  # 把这个图片的内容转化为这个pic变量
        # self.button = wx.BitmapButton(panel, -1, pic, pos=(10, 10))   # 以图片作为按钮，美化用
        self.btn_login.Bind(wx.EVT_BUTTON, self.login)  # 按钮事件绑定
        self.btn_regist = wx.Button(self, -1, '注册', pos=(300, 240), size=(70, 50))
        self.btn_regist.SetBackgroundColour('#28ac34')
        self.btn_regist.Bind(wx.EVT_BUTTON, self.regist)

    # 创建菜单栏
    def make_menu_bar(self):
        menubar = wx.MenuBar()  # 创建菜单栏
        self.SetMenuBar(menubar)  # 添加到窗口
        file_menu = wx.Menu()  # 创建一个菜单
        menubar.Append(file_menu, '菜单')
        self.Center()
        file_menu.AppendSeparator()  # 分割线
        menu_save = file_menu.Append(id=wx.ID_NEW, item='软件简介')  # 往菜单中添加一个菜单项
        self.Bind(wx.EVT_MENU, self.about, menu_save)  # 为此菜单项添加事件处理

    def about(self, event):
        info = wx.adv.AboutDialogInfo()
        info.Version = "1.0"
        info.Description = wordwrap(
            "目前已配置功能主要有以下：\n"
            "1.\n"
            "2.\n"
            "3.\n"
            "注册账号、密码无限制\n"
            "已注册的账号密码存储在Excel文件中(账号密码本.xlsx)",
            600, wx.ClientDC(self))
        info.Developers = ['lbf']
        info.License = wordwrap("blah " + "\n" +"blah ", 500, wx.ClientDC(self))
        wx.adv.AboutBox(info)

    def show_message(self, word=""):
        dlg = wx.MessageDialog(None, word, u"错误", wx.YES_NO | wx.ICON_QUESTION)
        if dlg.ShowModal() == wx.ID_YES:
            # self.Close(True)
            pass
        dlg.Destroy()

    # 登录
    # 设定密码错误3次即退出系统（赋予账号冻结时间功能再说）
    def login(self, event):
        username = self.entry_user.GetValue()
        password = self.entry_pass.GetValue()
        if username == '' or password == '':
            self.show_message(word='账号或密码为空')
        else:
            df = pd.read_excel('账号密码本.xlsx')
            if username in list(df['账号']):
                psw = df[df['账号'] == username]['密码'].values[0]
                if str(psw) == password:
                    identity = df[df['账号'] == username]['身份'].values[0]
                    home_frame = home.HomeFrame(identity)
                    home_frame.Show()
                    self.Destroy()
                else:
                    self.count -= 1
                    if self.count == 0:
                        self.log.WriteText('密码错误次数上限，即将退出')
                        self.Destroy()
                    else:
                        self.log.WriteText('密码错误，还剩%d次重试机会' % self.count)
            else:
                self.show_message(word='账号不存在，请先注册')

    # 注册
    def regist(self, event):
        rf = regist.RegistFrame(self)
        rf.CenterOnScreen()
        rf.Show()   # 让注册窗口显示出来


if __name__ == "__main__":
    app = wx.App()
    log = message_log.Log()
    frame = LoginFrame(log)
    frame.Show()
    app.MainLoop()
